package com.apollographql.apollo.internal.fetcher;

import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.fetcher.ResponseFetcher;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import com.apollographql.apollo.interceptor.ApolloInterceptorChain;
import java.util.concurrent.Executor;

public final class NetworkFirstFetcher implements ResponseFetcher {
    @Override // com.apollographql.apollo.fetcher.ResponseFetcher
    public ApolloInterceptor provideInterceptor(ApolloLogger logger) {
        return new NetworkFirstInterceptor(logger);
    }

    private static final class NetworkFirstInterceptor implements ApolloInterceptor {
        volatile boolean disposed;
        final ApolloLogger logger;

        NetworkFirstInterceptor(ApolloLogger logger2) {
            this.logger = logger2;
        }

        @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
        public void interceptAsync(final InterceptorRequest request, final ApolloInterceptorChain chain, final Executor dispatcher, final CallBack callBack) {
            chain.proceedAsync(request.toBuilder().fetchFromCache(false).build(), dispatcher, new CallBack() {
                /* class com.apollographql.apollo.internal.fetcher.NetworkFirstFetcher.NetworkFirstInterceptor.AnonymousClass1 */

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onResponse(InterceptorResponse response) {
                    callBack.onResponse(response);
                }

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onFailure(final ApolloException networkException) {
                    NetworkFirstInterceptor.this.logger.d(networkException, "Failed to fetch network response for operation %s, trying to return cached one", request.operation.name().name());
                    if (!NetworkFirstInterceptor.this.disposed) {
                        chain.proceedAsync(request.toBuilder().fetchFromCache(true).build(), dispatcher, new CallBack() {
                            /* class com.apollographql.apollo.internal.fetcher.NetworkFirstFetcher.NetworkFirstInterceptor.AnonymousClass1.AnonymousClass1 */

                            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                            public void onResponse(InterceptorResponse response) {
                                callBack.onResponse(response);
                            }

                            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                            public void onFetch(FetchSourceType sourceType) {
                                callBack.onFetch(sourceType);
                            }

                            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                            public void onFailure(ApolloException cacheException) {
                                callBack.onFailure(networkException);
                            }

                            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                            public void onCompleted() {
                                callBack.onCompleted();
                            }
                        });
                    }
                }

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onCompleted() {
                    callBack.onCompleted();
                }

                @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
                public void onFetch(FetchSourceType sourceType) {
                    callBack.onFetch(sourceType);
                }
            });
        }

        @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
        public void dispose() {
            this.disposed = true;
        }
    }
}
